package com.share.two.mail.receiveFromMQ;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.share.two.mail.entity.Mail;
import com.share.two.mail.util.EmailSendHelper;

@Service
public class MQListener implements SessionAwareMessageListener<Message>{
	Logger logger = Logger.getLogger("runLog");
	
	@Autowired
	EmailSendHelper emailSendHelper;
	
//	@Autowired
//	JmsTemplate jmsTemplate;

	@Override
	public void onMessage(Message message, Session session) throws JMSException {
		
		TextMessage textMessage = (TextMessage)message;
		String msg = textMessage.getText();
		logger.info("收到的消息为： " + msg);
		Mail mail = JSONObject.parseObject(msg, Mail.class);
		if (mail == null) {
			return;
		}
		
		try {
			String[] to = {mail.getTo()};
			String[] cs =  {};
			emailSendHelper.send(to, cs, mail.getSubject(), mail.getContent(), null);
			logger.info("email 发送成功");
		} catch (Exception e) {
			logger.error(e);
		}
		
	}

}
